﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Entity;
using HighLowBase.Entity;

namespace HighLowBase
{
    public class HiLoGame
    {
        public static GameBaseEntity LoadGameInfo(int gameId)
        {
            var hiLoGame = DB.HighLowGameInstance.uspHiLoGameSelect(gameId).FirstOrDefault();
            return new GameBaseEntity()
            {
                Id = hiLoGame.Id,
                Name = hiLoGame.Name,
                MinimumValue = hiLoGame.MinimumValue,
                MaximumValue = hiLoGame.MaximumValue,
                MinimumBet = (int)hiLoGame.MinimumBet,
                MaximumBet = (int)hiLoGame.MaximumBet,
                Rate = hiLoGame.Rate,
                RandomRate = hiLoGame.RandomRate,
                StartExtraLife = hiLoGame.StartExtraLife,
                StartOutOf = hiLoGame.StartOutOf,
                StartNudges = hiLoGame.StartNudges,
                StartFreeSpin = hiLoGame.StartFreeSpin,
                ExtraLife = hiLoGame.ExtraLife,
                OutOf = hiLoGame.OutOf,
                Nudges = hiLoGame.Nudges,
                FreeSpin = hiLoGame.FreeSpin,
                MaximumExtraLife = hiLoGame.MaximumExtraLife,
                MaximumOutOf = hiLoGame.MaximumOutOf,
                MaximumNudges = hiLoGame.MaximumNudges,
                MaximumFreeSpin = hiLoGame.MaximumFreeSpin,

                JackpotMinBetAu = (int)hiLoGame.JackpotMinBetAu,
                JackpotStartAu = (int)hiLoGame.JackpotStartAu,
                JackpotAu = (int)hiLoGame.JackpotAu,
                JackpotBlockAu = (int)hiLoGame.JackpotBlockAu,
                JackpotJumpAu = (int)hiLoGame.JackpotJumpAu,
                JackpotStatusAu = (int)hiLoGame.JackpotStatusAu,
                JackpotMinBetAg = (int)hiLoGame.JackpotMinBetAg,
                JackpotStartAg = (int)hiLoGame.JackpotStartAg,
                JackpotAg = (int)hiLoGame.JackpotAg,
                JackpotBlockAg = (int)hiLoGame.JackpotBlockAg,
                JackpotJumpAg = (int)hiLoGame.JackpotJumpAg,
                JackpotStatusAg = (int)hiLoGame.JackpotStatusAg

            };
        }

        public static int AddJackpotAuAg(int gameId, int au, int ag)
        {
            if (au <= 0 && ag <= 0)
                return 0;
            return DB.HighLowGameInstance.uspBaseHiLoGameAddJackpot(gameId, au, ag);
        }

        public static int ResetJackpot(int gameId, bool isResetAu, bool isResetAg)
        {
            return DB.HighLowGameInstance.uspBaseHiLoGameResetJackpot(gameId, isResetAu, isResetAg);
        }

        #region Game Triple 7

        public static Dictionary<int, RateConfigTriple7Entity> GetRateConfigTriple7DB()
        {
            Dictionary<int, RateConfigTriple7Entity> result = new Dictionary<int, RateConfigTriple7Entity>();
            // Cache ko co du lieu
            // Lay du lieu va set len cache
            int? gameId = 1;
            string gameName = string.Empty;
            int? minimumValue = 1;
            int? maximumValue = 13;
            double? rate = 95;
            var rateConfList = DB.HighLowGameInstance.uspTriple7GetRateConfig(ref gameId, ref gameName, ref minimumValue, ref maximumValue, ref rate).ToList();

            foreach (var item in rateConfList)
            {
                RateConfigTriple7Entity rateConfig = new RateConfigTriple7Entity()
                {
                    Location = item.Location.Value,
                    ProbabilityHigh = item.ProbabilityHigh.Value,
                    ProbabilityLow = item.ProbabilityLow.Value,
                    WinForHigh = item.WinForHigh.Value,
                    WinForLow = item.WinForLow.Value,
                    Rate = rate.Value
                };
                result.Add(rateConfig.Location, rateConfig);
            }
            return result;
        }

        public static Dictionary<int, RateConfigTriple7Entity> GetRateConfigTriple7(double currentRate, int value)
        {
            //Lay du lieu tren cache
            Dictionary<int, RateConfigTriple7Entity> result = HiLoRedis.GetRateConfigTriple7();

            if (result != null && result.Count() != 0 && result[value].Rate != currentRate)
                result = null;

            if (result == null)
                result = GetRateConfigTriple7DB();
            return result;
        }

        #endregion Game Triple 7

        public static List<HelpEntity> GetHelpInfoList(int hiLoGameId, int type)
        {
            int? total = 0;
            var result = HiLoRedis.GetHelpInfo(hiLoGameId, type);
            if (result != null)
                return result;
            
            var list = DB.HighLowGameInstance.uspHiLoHelpSelectBy(hiLoGameId, string.Empty, type, 1, 1, int.MaxValue - 1000, ref total).ToList();
            result = new List<HelpEntity>(list.Count);
            foreach (var item in list)
            {
                result.Add(new HelpEntity()
                {
                    Id = item.Id,
                    HiLoGameId = item.HiLoGameId,
                    Title = item.Title,
                    Content = item.Content,
                    Description = item.Description,
                    Type = item.Type
                });
            }
            HiLoRedis.SetHelpInfo(hiLoGameId, type, result);
            return result;
        }

        #region GameID
        public static class GameId
        {
            public static int Triple7Id = 1;
        }
        #endregion
    }
}
